﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Services;
using System.Text.RegularExpressions;
namespace paymentgateway
{
    /// <summary>
    /// Summary description for Service1
    /// </summary>
    [WebService(Namespace = "http://tempuri.org/")]
    [WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
    [System.ComponentModel.ToolboxItem(false)]
    // To allow this Web Service to be called from script, using ASP.NET AJAX, uncomment the following line. 
    // [System.Web.Script.Services.ScriptService]
    public class Service1 : System.Web.Services.WebService
    {

        [WebMethod]
        public ResultData Payment(Guid memberGuid,string cardHolder,string cardNumber,int cardExpiryMonth,int cardExpiryYear,string cardCvv)
        {
            /*
             *  memberId
                memberGuid: 	
                countryId: 	
                amount: 	
                currencyId: 	
                trackingMemberCode: 	
                cardNumber: 	
                cardHolder: 	
                cardExpiryMonth: 	
                cardExpiryYear: 	
                cardCvv: 	
                cardType: 	
                issueNumber: 	
                merchantAccountType: 	
                dynamicDescriptor: 	
                avsAddress: 	
                avsZip: 	
            */
            ResultData result=new ResultData();
            //4907639999990022,
            String cardHolderRegex = @"^[a-zA-Z\s]+$";
            string cardNumberRegex=@"^[0-9]{16}$";
            string cardCvvRegex = @"[0-9]{3}";
            if (Regex.IsMatch(cardHolder.Trim(), cardHolderRegex) 
                && Regex.IsMatch(cardNumber.Trim(), cardNumberRegex) 
                && Regex.IsMatch(cardCvv.Trim(), cardCvvRegex) 
                &&  cardExpiryYear>=Int32.Parse(DateTime.Now.Year.ToString()) 
                &&  (!(cardExpiryYear==Int32.Parse(DateTime.Now.Year.ToString()) && cardExpiryMonth<=Int32.Parse(DateTime.Now.Month.ToString()) ))  )
            {
                result.ResultCode = 1;
                result.ErrorMessage = "Transaction Successful.";
                result.TranId = DateTime.Now.Millisecond.ToString();
            }
            else
            {
                result.ResultCode = 100;
                result.ErrorMessage = "Transaction Error.";
                result.TranId = "";

            }
            return result;       
        }
    }
    public class ResultData {
        int _resultCode;
        string _errorMessage;
        string _TranId;
        public int ResultCode
        {
            get{return _resultCode;}
            set{_resultCode=value;}
        }
        public string ErrorMessage
        {
            get { return _errorMessage; }
            set { _errorMessage = value; }
        }
        public string TranId
        {
            get { return _TranId; }
            set { _TranId = value; }
        }
    }
}
